/**
 * Created by dio on 2017/2/28 0028.
 */
import React from "react";
import {ActivityIndicator, Image, InteractionManager, ScrollView, Text, TouchableOpacity, View} from "react-native";
import chjStyle from "../../CustomStyle";
import Toast from "react-native-root-toast";
import ChjGallery from "./components/ChjGallery";
import ModuleCommonHeader from "../module/ModuleCommonHeader";
import Util from "../Util";
import {Actions} from "react-native-router-flux";

export default class MoreCarReports extends React.Component {
    // 构造函数，可执行初始化操作
    constructor(props) {
        super(props);
        this.state = {
            isLoading: true
        };
    }

    // 组件被加载时
    componentDidMount() {
        InteractionManager.runAfterInteractions(() => {
            this.setState(
                {
                    isLoading: false
                }
            )
        });
    }

    // 组件即将被卸载时
    componentWillUnmount() {
    }

    // 组件被渲染时
    render() {
        if (this.state.isLoading) {
            if (this.state.isLoading) {
                return (
                    <View>
                        <ModuleCommonHeader title={this.props.title} leftText='返回'/>
                        <ActivityIndicator
                            animating={true}
                            style={{alignItems: 'center', justifyContent: 'center', height: chjStyle.height}}
                            size="large"/>
                    </View>);
            }
        }
        return (
            <View>
                <ModuleCommonHeader title={this.props.title} leftText='返回'/>
                <ScrollView
                    showsVerticalScrollIndicator={false}
                    style={[chjStyle.recordContainer, {backgroundColor: '#fff'}]}>
                    {
                        this.props.params.map(
                            (item, i) => this.renderItem(item, i)
                        )
                    }
                </ScrollView>
            </View>
        );
    }

    renderItem(item, i) {
        const val = item.keyvalue;
        let isNormal = true;
        let exception = null;
        if (val === '正常' || val === '无' || val === '是' || val.length === 0) {
        }
        else if (val.indexOf('#') !== -1) {
            const imgs = [];
            exception = {};
            exception.name = '';

            const dataModel = Util.decodePhotoString(val);
            for (let j = 0; j < dataModel.length; j++) {
                const node = dataModel[j];
                if (node.remotePic !== 'null') {
                    imgs.push({bimg: Util.ImgMiddleServicePath + node.remotePic});
                    isNormal = false;
                    exception.name += node.name + ' ';
                }
            }
            exception.imgs = imgs;
        } else {
            exception = {};
            exception.msg = val;
            isNormal = false;
        }

        const statusIcon = isNormal ? require('../../../img/normalIcon.png') : require('../../../img/error.png');

        return (
            <TouchableOpacity
                onPress={() => this.showException(exception)}
                style={[chjStyle.textItem]}>
                <Text
                    style={{
                        position: 'absolute',
                        color: '#303030',
                        textAlign: 'left',
                        backgroundColor: 'rgba(0, 0, 0, 0)',
                        fontSize: chjStyle.getActualFontSize(30),
                        top: chjStyle.getActualSizeInVertical(30),
                        left: chjStyle.getActualSizeInHorizontal(40),
                        width: chjStyle.getActualSizeInHorizontal(500),
                    }}>{item.keyname}</Text>
                <Image source={statusIcon} resizeMode='contain' style={{
                    position: 'absolute',
                    right: chjStyle.getActualSizeInHorizontal(30),
                    top: chjStyle.getActualSizeInVertical(45),
                    width: chjStyle.getActualSizeInHorizontal(30),
                    height: chjStyle.getActualSizeInVertical(30)
                }}/>
            </TouchableOpacity>);
    }

    showException(exception) {
        if (exception) {
            if (exception.imgs && exception.imgs.length > 0) {
                Actions.ChjGallery(
                    {
                        name: 'ChjGallery',
                        images: exception.imgs,
                        title: exception.name,
                    }
                );
            }
            else {
                Toast.show(exception.msg + ' 异常', {
                    duration: Toast.durations.SHORT,
                    position: Toast.positions.CENTER
                });
            }
        }
    }
}

